#include "OpenGLWidgetTextue.h"
#include<QDebug>

#include<gl/GLU.h>
#include<QCoreApplication>
#include<QBitmap>
// 纹理贴图测试。

static float vertices[] = {
	0.5f, 0.5f, 0.0f,  // top right
	0.5f,-0.5f, 0.0f, // bottom right
	-0.5f, -0.5f, 0.0f, // bottom left
	-0.5f, 0.5f, 0.0f // top left
};

static unsigned int indices[] = {
	0,1,2,3, // 四边形 

};

static float texCoordVertexs[] = {
1,0, // 图片右上角
1,1, // 右下角
0,1, // 左下角
0,0 // 左上角
};

OpenGLWidgetTest::OpenGLWidgetTest(QWidget* parent) :QOpenGLWidget(parent)
{}

OpenGLWidgetTest::~OpenGLWidgetTest()
{
	//makeCurrent();
	//glDeleteBuffers(1, &VBO);
	//glDeleteVertexArrays(1, &VAO);
	//glDeleteProgram(shaderProgram);
	//doneCurrent();
}

//void OpenGLWidgetTest::drawShape(Shape shape)
//{
//	_shape = shape;
//	update();
//}

void OpenGLWidgetTest::setWireFrame(bool wireFrame)
{
	// 在paintGL 函数以外的地方调用OpenGL函数没有意义，最终会被paintGL覆盖。所以必须以如下方式调用OpenGL函数。
	makeCurrent(); // --- 外部调用OpenGL函数前必须以 makeCurrent 函数开始
	if (wireFrame)
	{
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}
	else
	{
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	}
	update();     // --- 外部调用OpenGL函数之后必须以 update 函数更新UI界面
	doneCurrent();// --- 外部调用OpenGL函数之后必须以 doneCurrent 函数结束
	//this->se
}
static GLuint texName;
void OpenGLWidgetTest::initializeGL()
{
	initializeOpenGLFunctions(); // 初始化opengl 相关的函数
	
	QString imgPath = QCoreApplication::applicationDirPath() +  "/../../resources/texture_test.png";
	QImage img(imgPath);
	QImage img2 = img.convertToFormat(QImage::Format::Format_RGB888);
	//img2 = img2.mirrored();

	//if (!img2.save("d:/bbb.png"))
	//{
	//	qDebug() << "save img2 error!";
	//}
	int w = img2.width();
	int h = img2.height();
	int len = img2.width() * img2.height() * 3;

	uchar* data = new uchar[len];
	memcpy(data, img2.bits(), len);

	//QBitmap bm = QBitmap::fromData(QSize(w, h), data, QImage::Format::Format_RGB888);
	//bm.fromData(QSize(w, h), data, QImage::Format::Format_RGB888);
	//bm.save("d:/aaa.png");

	/*QImage img3 (w,h,QImage::Format::Format_RGB888);
	memcpy(img3.bits(), data, len);

		if (img3.save("d:/aaa.png"))
		{
			qDebug() << "save img error!";
		}
	
	
	qDebug() << img3.format();*/
	qDebug() << img2.format();
	glGenTextures(1, &texName);
	glBindTexture(GL_TEXTURE_2D, texName);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	glGenerateMipmap(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 0);

}

void OpenGLWidgetTest::resizeGL(int w, int h)
{
}

void OpenGLWidgetTest::paintGL()
{
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	qDebug() << "paintGL";
	
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, vertices);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texName);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glTexCoordPointer(2,GL_FLOAT,0, texCoordVertexs);

	//glDrawArrays(GL_TRIANGLES, 0 , 3);
	glDrawElements(GL_QUADS, 4,GL_UNSIGNED_INT , indices);

}
